package service

import (
	context "context"
	"fmt"
	"net"

	"google.golang.org/grpc"
)

type grpcServeice struct {
}
type Judge struct{}

func (j *Judge) HyJudge(ctx context.Context, req *JudgeRquest) (*JudgeRespon, error) {

	res := CompileObj.Compiles(req.Code, req.LuangueType)

	return &JudgeRespon{Result: res.Result}, nil
}

func NewGrpcService() *grpcServeice {

	return &grpcServeice{}
}

func (g *grpcServeice) StartService(port int) {

	Nets, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
	if err != nil {
		panic(err)
	}
	fmt.Println("Grpc 服务已经启动 Port:", port)
	defer fmt.Println("服务发生了异常！！\n 服务已经关闭")

	s := grpc.NewServer()
	RegisterJudgeServerServer(s, &Judge{})

	go fmt.Println("JudgeService....\nService Address=127.0.0.1:8080")

	s.Serve(Nets)

}
